﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

public interface IShape
{
    double CalculateArea();
    bool IsValid();
}
public abstract class Shape : IShape 
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}
//长方形类：
public class Rectangle : Shape
{//长、宽
    public double Width { get; set; }
  public double Height { get; set; }
    //构造函数
    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }
    //计算面积
    public override double CalculateArea()
    {
        return Width * Height;
    }
    //查看是否有效
    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}
//正方形：
public class Square : Shape
{ //边长：
    public double Side { get; set; }
    //构造函数：
    public Square(double side)
    {
        Side = side;
    }
    //面积：
    public override double CalculateArea()
    {
        return Side * Side;
    }
    //有效：
    public override bool IsValid()
    {
        return Side > 0;
    }
}
//三角形：
public class Triangle : Shape 
{ //三条边：
    public double SideA { get; set; }
    public double SideB { get; set; }
    public double SideC { get; set; }
    //构造函数：
    public Triangle(double sideA, double sideB, double sideC)
    {
        SideA = sideA;
        SideB = sideB;
        SideC = sideC;
    }
    //计算面积：
    public override double CalculateArea()
    {
        double m = (SideA + SideB + SideC) / 2;
        return Math.Sqrt(m * (m - SideA) * (m - SideB) * (m - SideC));
    }
    //判断是否有效：
    public override bool IsValid()
    {
        return ((SideA + SideB > SideC) && (SideA + SideC > SideB) && (SideB + SideC > SideA));
    }
}
namespace Shape_S
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请选择形状：");
            Console.WriteLine("1.长方形   2.正方形  3.三角形");
            string s = Console.ReadLine();
            if (s == null)
            {
                Console.WriteLine("请选择一个形状！");
                s = Console.ReadLine();
            }
            else
            {if (s == "1")
                {
                    Console.WriteLine("你选择的形状是  长方形");
                    Console.WriteLine("请输入长方形的长：");
                    string Wid = Console.ReadLine();
                    double InWidth = double.Parse(Wid);
                    Console.WriteLine("请输入长方形的宽：");
                    string Hei = Console.ReadLine();
                    double InHei = double.Parse(Hei);
                    Rectangle rectangle = new Rectangle(InWidth, InHei);
                    Console.WriteLine("是否有效："+rectangle.IsValid());
                    if(rectangle.IsValid())
                    { Console.WriteLine("面积：" + rectangle.CalculateArea()); }
                    else
                    {
                        Console.WriteLine("没有有效面积！");
                    }
                }
            if (s == "2")
                {
                    Console.WriteLine("你选择的形状是  正方形");
                    Console.WriteLine("请输入正方形的边长：");
                    string Side = Console.ReadLine();
                    double InSide = double.Parse(Side);
                    Square square = new Square(InSide);
                    Console.WriteLine("是否有效：" + square.IsValid());
                    if (square.IsValid())
                    { Console.WriteLine("面积：" + square.CalculateArea()); }
                    else
                    {
                        Console.WriteLine("没有有效面积！");
                    }
                }
            if (s=="3")
                {
                    Console.WriteLine("你选择的形状是  三角形");
                    Console.WriteLine("请输入三角形的第一条边长：");
                    string a = Console.ReadLine();
                    double A = double.Parse(a);
                    Console.WriteLine("请输入三角形的第二条边长：");
                    string b = Console.ReadLine();
                    double B= double.Parse(b);
                    Console.WriteLine("请输入三角形的第三条边长：");
                    string c= Console.ReadLine();
                    double C= double.Parse(c);
                    Triangle triangle = new Triangle(A,B,C);
                    Console.WriteLine("是否有效：" + triangle.IsValid());
                    if (triangle.IsValid())
                    { Console.WriteLine("面积：" + triangle.CalculateArea()); }
                    else
                    {
                        Console.WriteLine("没有有效面积！");
                    }
                }
            //便于查看结果
                Console.ReadLine();
            }
        }
    }
}
